package com.danceforyou.common.util;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.hssf.util.Region;

import javax.servlet.http.HttpServletResponse;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;


/**
 * excel工具类
 * Created by zhijun on 2015/12/28.
 */
public class ExcelUtil {

    private String title;//标题
    private String[] rowName ;//列名
    private List<Object[]> dataList = new ArrayList<Object[]>();//数据
    private HttpServletResponse res;

    private final Integer MAX_ROW=1000;//最大行数
    private  Integer sheetNum=0;//sheet数
    private HSSFWorkbook workbook=null;
    private int titleNum;//列头所跨列数-1
    private short[] editwidth;//设置列宽
    private short[] numberCell;//数字格式单元格

    public ExcelUtil(String title,String[] rowName,List  dataList,HttpServletResponse res,int titleNum,short[] editwidth,short[] numberCell){
        this.dataList = dataList;
        this.rowName = rowName;
        this.title = title;
        this.res=res;
        this.titleNum=titleNum;
        this.editwidth=editwidth;
        this.numberCell=numberCell;
    }

    /**
     * 创建工作簿对象
     * @return
     */
    public HSSFWorkbook createWorkkbook(){
        if(workbook==null){
            workbook=new HSSFWorkbook();
        }
        return workbook;
    }

    /**
     *
     * 创建工作表
     * @param workbook
     * @return
     */
    public HSSFSheet createSheet(HSSFWorkbook workbook){
        HSSFSheet sheet = workbook.createSheet(title+sheetNum+1);
        for(short i=0;i<editwidth.length;i++){
            sheet.setColumnWidth(i, editwidth[i]*1000);
        }
        HSSFRow rowm = sheet.createRow(0);

        HSSFCell cellTiltle = rowm.createCell((short)0);
        HSSFCellStyle columnTopStyle = this.getColumnTopStyle(workbook);//获取列头样式对象
        if (title != null){
            sheet.addMergedRegion(new Region((short)0, (short)0, (short)1, (short)(titleNum)));
            cellTiltle.setCellStyle(columnTopStyle);
            cellTiltle.setCellValue(title);
        }

        // 定义所需列数
        int columnNum = rowName.length;
        HSSFRow rowRowName = sheet.createRow(2);                // 在索引2的位置创建行(最顶端的行开始的第二行)

        // 将列头设置到sheet的单元格中
        for(short n=0;n<columnNum;n++){
            HSSFCell  cellRowName = rowRowName.createCell(n);               //创建列头对应个数的单元格
            cellRowName.setCellType(HSSFCell.CELL_TYPE_STRING);             //设置列头单元格的数据类型
            cellRowName.setCellValue(rowName[n]);                              //设置列头单元格的值
            cellRowName.setCellStyle(columnTopStyle);                       //设置列头单元格样式
        }
        return sheet;
    }
    /**
     * 分sheet导出Workbook对象
     */
    public void fillWorkbook(){
        HSSFSheet sheet=this.createSheet(this.createWorkkbook());
        HSSFCellStyle style = this.getStyle(workbook);
        HSSFCellStyle style1 = this.getStyle1(workbook); //单元格样式对象
        //将查询出的数据设置到sheet对应的单元格中
        int dsize=0;
        if(dataList.size()>0){
            dsize=dataList.get(0).length;
        }
//        for(int i=MAX_ROW*sheetNum;i<(dataList.size()<100?100:dataList.size());i++){
        for(int i=MAX_ROW*sheetNum;i<dataList.size();i++){
            if(i<MAX_ROW*(sheetNum+1)) {
                Object[] obj = new Object[dsize];
                if(dataList.size()>i){
                    obj = dataList.get(i);
                }
                HSSFRow row = sheet.createRow(i%MAX_ROW + 3);//创建所需的行
                for (short j = 0; j < obj.length; j++) {
                    HSSFCell cell = row.createCell(j);
                    if (!"".equals(obj[j]) && obj[j] != null) {
                        if (numberCell[j] == new Short("1")){
                            cell.setCellValue(Double.valueOf(obj[j].toString()));
                        }else {
                            cell.setCellValue(obj[j].toString());
                        }
                        cell.setCellStyle(style);
                    }else{
                        cell.setCellValue("");
                        cell.setCellStyle(style);
                    }
                    if(j==0){
                        cell.setCellType(HSSFCellStyle.ALIGN_LEFT);
                    }
                }
            }else {
                sheetNum++;
                fillWorkbook();
                break;
            }
        }
    }

    /**
     * 导出
     */
    public  void export(){
        String fileName = "Excel"+String.valueOf(System.currentTimeMillis()).substring(4, 13) + ".xls";
        String headStr = "attachment; filename=\"" + fileName + "\"";
        res.reset();
        res.setContentType("application/msexcel");
        res.setHeader("Content-disposition", headStr);
        res.addHeader("Pargam", "no-cache");
        res.addHeader("Cache-Control", "no-cache");
        try {
            OutputStream out = res.getOutputStream();
            workbook.write(out);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
     * 列头单元格样式
     */
    public HSSFCellStyle getColumnTopStyle(HSSFWorkbook workbook) {
        // 设置字体
        HSSFFont font = workbook.createFont();
        //设置字体大小
        font.setFontHeightInPoints((short)12);
        //字体加粗
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        //设置字体名字
        font.setFontName("微软雅黑");
        //设置样式;
        HSSFCellStyle style = workbook.createCellStyle();
        //设置底边框;
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        //设置底边框颜色;
        style.setBottomBorderColor(HSSFColor.BLACK.index);
        //设置左边框;
        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        //设置左边框颜色;
        style.setLeftBorderColor(HSSFColor.BLACK.index);
        //设置右边框;
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);
        //设置右边框颜色;
        style.setRightBorderColor(HSSFColor.BLACK.index);
        //设置顶边框;
        style.setBorderTop(HSSFCellStyle.BORDER_THIN);
        //设置顶边框颜色;
        style.setTopBorderColor(HSSFColor.BLACK.index);
        //在样式用应用设置的字体;
        style.setFont(font);
        //设置自动换行;
        style.setWrapText(false);
        //设置水平对齐的样式为居中对齐;
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        //设置垂直对齐的样式为居中对齐;
        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        return style;
    }
    /*
     * 列数据信息单元格样式
     */
    public HSSFCellStyle getStyle1(HSSFWorkbook workbook) {
        // 设置字体
        HSSFFont font = workbook.createFont();
        //设置字体大小
        //font.setFontHeightInPoints((short)10);
        //字体加粗
        //font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        //设置字体名字
        font.setFontName("微软雅黑");
        //设置样式;
        HSSFCellStyle style = workbook.createCellStyle();
        //在样式用应用设置的字体;
        style.setFont(font);
        //设置自动换行;
        style.setWrapText(false);
        //设置水平对齐的样式为居中对齐;
        //style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        //设置垂直对齐的样式为居中对齐;
        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        return style;
    }
    /*
     * 列数据信息单元格样式
     */
    public HSSFCellStyle getStyle(HSSFWorkbook workbook) {
        // 设置字体
        HSSFFont font = workbook.createFont();
        //设置字体大小
        //font.setFontHeightInPoints((short)10);
        //字体加粗
        //font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        //设置字体名字
        font.setFontName("微软雅黑");
        //设置样式;
        HSSFCellStyle style = workbook.createCellStyle();
        //设置底边框;
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        //设置底边框颜色;
        style.setBottomBorderColor(HSSFColor.BLACK.index);
        //设置左边框;
        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        //设置左边框颜色;
        style.setLeftBorderColor(HSSFColor.BLACK.index);
        //设置右边框;
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);
        //设置右边框颜色;
        style.setRightBorderColor(HSSFColor.BLACK.index);
        //设置顶边框;
        style.setBorderTop(HSSFCellStyle.BORDER_THIN);
        //设置顶边框颜色;
        style.setTopBorderColor(HSSFColor.BLACK.index);
        //在样式用应用设置的字体;
        style.setFont(font);
        //设置自动换行;
        style.setWrapText(false);
        //设置水平对齐的样式为居中对齐;
        //style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        //设置垂直对齐的样式为居中对齐;
        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        return style;
    }
}
